package com.dididoctor.doctor.Receive;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;

import com.alibaba.fastjson.JSON;
import com.dididoctor.doctor.Activity.mvp.MainActivity;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.WYY.CustomAttachmentType;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.msg.MsgServiceObserve;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;

import java.util.List;
import java.util.Random;


/**
 * XMPP 退出登录广播
 *
 * @author nseer
 */
public class WYXYReceier extends BroadcastReceiver {
    private Context context;
    private NotificationManager manager;
    private Notification notification;
    private Vibrator mVibrator01;  //声明一个振动器对象
    private PendingIntent pintent;
    private String des;
    private static final Random random = new Random(System.currentTimeMillis());

    @Override
    public void onReceive(final Context context, Intent intent) {
        this.context = context;
        manager = (NotificationManager) this.context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Observer<List<IMMessage>> incomingMessageObserver =
                new Observer<List<IMMessage>>() {
                    @Override
                    public void onEvent(List<IMMessage> messages) {
                        // 处理新收到的消息，为了上传处理方便，SDK 保证参数 messages 全部来自同一个聊天对象。
                        for (int x = 0; x < messages.size(); x++) {
                            if (messages.get(x).getMsgType() == MsgTypeEnum.custom) {
                                MsgAttachment attachment = messages.get(x).getAttachment();
                                if (attachment != null) {
                                    com.alibaba.fastjson.JSONObject object = JSON.parseObject(attachment.toJson(false));

                                    if (Integer.parseInt(object.get("messageAttachType").toString()) == CustomAttachmentType.RTS) {
                                        //点击通知栏跳转的界面设置
                                        Intent intent1 = null;
                                        intent1 = new Intent(context, MainActivity.class);
                                        intent1.putExtra("CurrentTab", 1);

                                        //延迟意图设置进入通知栏用
                                        pintent = PendingIntent.getActivity(context, random.nextInt(),
                                                intent1, PendingIntent.FLAG_UPDATE_CURRENT);

                                        des = "用户"+object.get("patientName").toString()+"向你发送了图文咨询申请";
                                        //发送通知
                                        sendNotification(des, pintent);
                                    } else if (Integer.parseInt(object.get("messageAttachType").toString()) == CustomAttachmentType.MEDICCALADVICE) {

                                    } else if (Integer.parseInt(object.get("messageAttachType").toString()) == CustomAttachmentType.APPOINTMENTPHONE) {
                                        //点击通知栏跳转的界面设置
                                        Intent intent1 = null;
                                        intent1 = new Intent(context, MainActivity.class);
                                        intent1.putExtra("CurrentTab", 1);

                                        //延迟意图设置进入通知栏用
                                        pintent = PendingIntent.getActivity(context, random.nextInt(),
                                                intent1, PendingIntent.FLAG_UPDATE_CURRENT);

                                        des = "用户"+object.get("patientName").toString()+"向你发送了电话预约申请";
                                        //发送通知
                                        sendNotification(des, pintent);
                                    } else if (Integer.parseInt(object.get("messageAttachType").toString()) == CustomAttachmentType.TIMELYPHONE) {

                                    }
                                }
                            } else if (messages.get(x).getMsgType() == MsgTypeEnum.tip) {

                            } else if (messages.get(x).getMsgType() == MsgTypeEnum.text) {

                            } else if (messages.get(x).getMsgType() == MsgTypeEnum.image) {

                            } else if (messages.get(x).getMsgType() == MsgTypeEnum.audio) {

                            }
                        }
                    }
                };
        NIMClient.getService(MsgServiceObserve.class)
                .observeReceiveMessage(incomingMessageObserver, true);
    }

    private void sendNotification(String des, PendingIntent intent) {
        notification = new Notification();
        notification.icon = R.drawable.applogos; // 显示的图标
        notification.defaults = Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.when = System.currentTimeMillis();

        //设置标题  和内容
        String title = "滴滴医生";


        /** 设置通知消息 */
        //需要标题和内容   title通知的标题  des通知的内容
        notification.setLatestEventInfo(context, title, des, intent);

        manager.notify(0, notification);
        //消息振动器
        mVibrator01 = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
        mVibrator01.vibrate(new long[]{100, 10, 100, 1000}, -1);
    }

}
